$(document).ready(function(){
	common.body_to_all();
	more_js.bind();
	more_js.init();
		$("#page_title").html("医生详情");
});
/**
 * 数组排重
 * 使用方式
 * var array = new Array(...);
 * var new_array = array.unique();
 * 附加排序方法：
 * arr.sort(function(a,b){return a-b;})
 */
Array.prototype.unique = function()
{
	this.sort();
	var re=[this[0]];
	for(var i = 1; i < this.length; i++)
	{
		if( this[i] !== re[re.length-1])
		{
			re.push(this[i]);
		}
	}
	return re;
}
var more_js = {};
more_js.obj = {
	//id
	id: 1,
	//姓名
	name: '李荣杰',
	//职业类型
	job_type: "主任医师",
	//图片头像
	img_url: img_pix + "doctor.jpg",
	//所属医院
	hospital: "广东医科大学第一附属医院",
	//负责领域
	domain: "心血管内科",
	//擅长
	good: '专科医生与合康云智能血压监测系统，观察治疗（用药）效果，及时发现恶性血压波动，并向患者发出APP推送警示',
	//医生类型 1：医生 2：营养师
	doctor_type: 1,
	//医生的服务项目
	service_list:[
		{
			//id
			id:1,
			//名称
			name:"血压管理",
		},
		{
			//id
			id:2,
			//名称
			name:"痛风管理",
		},	
	]
};
more_js.bind = function(){
}
more_js.init = function(){
	vm.load();
	vm.load_time();
}
var vm = avalon.define({
	//标记是否显示遮罩
	is_mask_show:true,
	is_basic_load:false,
	is_time_load:false,
	//关闭遮罩
	close_mask:function(){
		if(vm.is_basic_load && vm.is_time_load){
			vm.is_mask_show = false;
		}
	},
	$id:"main_controller",
	
	//医生数据
	obj:{
		//id
		id: 0,
		//姓名
		name: '',
		//职业类型
		job_type: "",
		//图片头像
		img_url: '',
		//所属医院
		hospital: '',
		//负责领域
		domain: '',
		//擅长
		good: '',
		//医生类型 1：医生 2：营养师
		doctor_type: 1,		
		//医生的服务项目
		service_list:[],		
		//门店地址
		storename:'',		
	},
	/*
	 * 加载数据
	 * 参数：id
	 */
	load:function(){
		$.ajax({
			type:"post",
			url:get_doctor_info_url,
			dataType:"json",
			success:function(data){
				vm.is_basic_load = true;
				vm.close_mask();				
				console.log(data);	
				vm.obj.id = data.dictuserid;
				vm.obj.name = data.echoname;
				vm.obj.good = data.goodatremark;
				vm.obj.job_type = data.store;
				vm.obj.doctor_type = data.usertype;
				vm.obj.img_url = data.userimage;
				vm.obj.hospital = data.infirmary;
				vm.obj.domain = data.office;
				vm.obj.storename = data.storename;
				var service_list = data.serve;
				$.each(service_list, function(index) {    
					 service_list[index].check = false;                                                         
				});
				vm.obj.service_list = service_list;
			},	
			error:function(){
				vm.is_mask_show = false;
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();					
			},		
			data:"dictuserid="+common.getUrlParam('id'),
		});			
	},
	//选择一项服务
	check_service:function(index){
		vm.obj.service_list[index].check = !vm.obj.service_list[index].check; 
	},
	/**
	 * 这个是要在页面上遍历用的集合
	 * 
	 */
	times:[],
	//加载医生的排班时间
	load_time:function(){
		$.ajax({
			type:"post",
			url:get_doctor_arrange_url,
			dataType:"json",
			success:function(data){
				vm.is_time_load = true;
				vm.close_mask();						
				console.log(data);	
				if(data.length<=0){
					vm.times = [];
					return ;
				}
				var dates_temp = [];
				$.each(data, function(index) {  
					data[index].is_check = false;
					dates_temp.push(data[index].regdate);
				});
				//排重
				dates_temp = dates_temp.unique();
				var dates = [];
				$.each(dates_temp, function(index) {
					var date = dates_temp[index];
					//根据日期求出星期几
					var dayOfWeek = new Date(date).getDay();
					var dayOfWeekValue = "";
					switch (dayOfWeek){
						case 0:
							dayOfWeekValue = "周日";
							break;						
						case 1:
							dayOfWeekValue = "周一";
							break;
						case 2:
							dayOfWeekValue = "周二";
							break;
						case 3:
							dayOfWeekValue = "周三";
							break;
						case 4:
							dayOfWeekValue = "周四";
							break;
						case 5:
							dayOfWeekValue = "周五";
							break;		
						case 6:
							dayOfWeekValue = "周六";
							break;								
						default:
							//没逻辑
							break;
					}
					dates.push({
						date:date,
						dayOfWeekValue:dayOfWeekValue,
						morning:[],
						afternoon:[],
						night:[],
					});
				});
				console.log(dates);
				//收集上午、下午、晚上的情况
				$.each(dates, function(index) {
					for(var i=0;i<data.length;i++){
						if(dates[index].date==data[i].regdate){
							if(data[i].libraryname == "上午"){
								dates[index].morning.push(data[i]);
							}
							if(data[i].libraryname == "下午"){
								dates[index].afternoon.push(data[i]);
							}				
							if(data[i].libraryname == "晚班"){
								dates[index].night.push(data[i]);
							}								
						}						
					}
				});
				console.log("dates",dates);
				vm.times = dates;
			},	
			error:function(){
				vm.is_mask_show = false;
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();					
			},		
			data:"dictuserid="+common.getUrlParam('id')+"&dictstoreid="+common.getUrlParam('dictstoreid'),
		});			
	},
	//标记是否已经选择了日期
	$is_select_date:false,
	$dictlibraryid:0,
	$dictuserschedid:0,
	$date:'',
	/**
	 * 选择日期
	 * 这里控制只能单选
	 * @param {Object} dictlibraryid
	 * @param {Object} dictuserschedid
	 * @param {Object} date
	 */
	select_date:function(dictlibraryid,dictuserschedid,date){
		vm.$is_select_date = true;
		vm.$dictlibraryid = dictlibraryid;
		vm.$dictuserschedid = dictuserschedid;
		vm.$date = date;
		$.each(vm.times, function(index) {    
			 var m = vm.times[index].morning;
			 var a = vm.times[index].afternoon;
			 var n = vm.times[index].night;
			 $.each(m, function(j) {    
			 	if(m[j].dictlibraryid == dictlibraryid && m[j].dictuserschedid == dictuserschedid){
			 		m[j].is_check = true;
			 	}else{
			 		m[j].is_check = false;
			 	}
			 });
			 $.each(a, function(j) {    
			 	if(a[j].dictlibraryid == dictlibraryid && a[j].dictuserschedid == dictuserschedid){
			 		a[j].is_check = true;
			 	}else{
			 		a[j].is_check = false;
			 	}
			 });
			 $.each(n, function(j) {    
			 	if(n[j].dictlibraryid == dictlibraryid && n[j].dictuserschedid == dictuserschedid){
			 		n[j].is_check = true;
			 	}else{
			 		n[j].is_check = false;
			 	}
			 });			 
		});
	},
	//追加变量防止重复提交
	$is_do:false,	
	/**
	 * 提交
	 */
	submit:function(){
		if(vm.$is_do){
			return ;
		}		
		//验证有没有选择服务项目
		var is_select_service = false;
		for(var i=0;i<vm.obj.service_list.size();i++){
			if(vm.obj.service_list[i].check){
				is_select_service = true;
				break;
			}
		}
		if(is_select_service == false){
			vm.toast_msg = "请选择服务项目";
			vm.is_toast_show = true;
			vm.clear_toast();	
			return ;	
		}
		//验证有没有选择了时间
		if(vm.$is_select_date == false){
			vm.toast_msg = "请选择预约时间";
			vm.is_toast_show = true;
			vm.clear_toast();	
			return ;	
		}
		var fd = new FormData();
		//门店ID
		fd.append('dictstoreid',common.getUrlParam('dictstoreid'));
		//医生ID
		fd.append('dictuserid',common.getUrlParam('id'));
		//指2015-10-20
		fd.append('regdate',vm.$date);
		//时间段ID
		fd.append('dictlibraryid',vm.$dictlibraryid);
		//排班ID
		fd.append('dictuserschedid',vm.$dictuserschedid);
		//服务名称，多个用,隔开
		var dictservices = '';
		//服务项集合
		var serve = [];			
		for(var i=0;i<vm.obj.service_list.size();i++){
			if(vm.obj.service_list[i].check){
				dictservices += ( vm.obj.service_list[i].productsname+"," );
				serve.push(vm.obj.service_list[i].dictproductsid);
			}
		}	
		fd.append('dictservices',dictservices);
		fd.append('serve',JSON.stringify(serve));
		var $this = $(this);
		$this.button('loading');
		$.ajax({
			type:"post",
			url:add_serve_url,
			dataType:"json",
			data:fd,			
			processData: false,
			contentType: false,			
			success:function(data){
				$this.button('reset');
				if(data.status == 1){
					vm.$is_do = true;
					vm.toast_msg = "操作成功";
					setTimeout(function(){
						location.href = index_url;
					},1000);
				}else if(data.status == 2){
					vm.toast_msg = "操作失败";
				}else if(data.status == 3){
					vm.toast_msg = "服务器异常";
				}else if(data.status == 4){
					vm.toast_msg = "预约满了";
				}
				vm.is_toast_show = true;
				vm.clear_toast();						
			},	
			error:function(){
				$this.button('reset');
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();					
			},			
		});		
	},
	//----------------
	//toast的信息
	toast_msg:'',
	//是否要展示toast
	is_toast_show:false,
	//定时器
	$timeout:0,
	//消息隐藏
	clear_toast:function(){
		//设置定时器
		if(vm.$timeout){
			clearTimeout(vm.$timeout);
		}
		vm.$timeout = setTimeout(function(){
			vm.is_toast_show = false;
			vm.toast_msg = '';
		},3000);		
	},	
	//-----------------	
	
});
avalon.scan();
avalon.ready(function(){
	$('body').css('opacity',1);
});
